library(lattice)
library(foreign)
library(reshape2)
library(MASS)
library(latticeExtra)
library(ggplot2)
library(ggthemes)

#######################
######################
#### means of notability 
#######################
######################

nm1 <- read.csv("notability means2.csv")
nm1$person <- reorder(nm1$person, nm1$mean)
xyplot(person ~ mean, 
       type = "d",
       data = nm1,
       aspect = 1.5,
       xlim = c(1.37, 2.7),
       xlab = "Average Familiarity",
       ylab = "",
       panel = function (x, y) {
         panel.xyplot(x, y, pch = 16, col = "black")
         panel.lines(x = 0, y = -1:7, col = "grey", lty = 2)
         panel.segments(nm1$lb, as.numeric(y),
                        nm1$ub, as.numeric(y), lty = 1, col = "black")} )



##################
##################
##################
## proportion DK responses
##################
##################
##################

pdk <- read.csv("propDK.csv")
pdk$person <- reorder(pdk$person, pdk$prop)

barchart(person~prop,
         data = pdk,
         aspect = 1,
         xlab = "Proportion Don't Know")



##############################
##############################
###############################
## effects plot
##############################
##############################

ep <- read.csv("effects plot.csv")

xyplot(est + lb + ub ~ affect | reg,
       data = ep,
       type = "l",
       lty = c(1,2,2),
       col = "black",
       aspect = 1,
       ylab = "Change in Diffuse Support",
       xlab = "Affect",
       panel = function (x, y,...) {
         panel.xyplot(x, y,...)
         panel.lines(x = -1:11, y = 0, col = "grey")
         
       } 
)

##############################
##############################
##############################
## effects plot of change in diffuse
## support across affect
## for control group 
##############################
##############################
##############################

controlAf <- read.csv("effect on control.csv")
controlAf$person <- reorder(controlAf$person, controlAf$est)

dotplot(person ~ est, 
        data = controlAf,
        aspect = 1.5,
        xlim = c(-.12, .12),
        xlab = "Affect Coefficient",
        panel = function (x, y) {
          panel.xyplot(x, y, pch = 16, col = "black")
          panel.lines(x = 0, y = -1:7, col = "grey", lty = 2)
          panel.segments(controlAf$lb, as.numeric(y),
                         controlAf$ub, as.numeric(y), lty = 1, col = "black")} )


#######################
######################
#### means of notability  
#######################
######################

nm <- read.csv("notability means.csv")
nm$person <- reorder(nm$person, nm$mean)
xyplot(person ~ mean,
       groups = sig,
       data = nm,
       aspect = 1.5,
       xlim = c(1.37, 3.86),
       pch = c(16,17), 
       xlab = "Average Familiarity",
       ylab = "",
       panel = function (x, y,...) {
         panel.xyplot(x, y,col = "black",...)
         #panel.lines(x = 1.9112, y = -1:13, col = "grey", lty = 2)
         panel.lines(x = 2.6055, y = -1:13, col = "grey", lty = 2)
         #panel.lines(x = 3.299, y = -1:13, col = "grey", lty = 3)
         panel.segments(nm$lb, as.numeric(y),
                        nm$ub, as.numeric(y), lty = 1, col = "black")} )




#########################
#########################
### affect densities for all 12 
#########################
#########################

ad <- read.csv("affect for all 12.csv")

ad <- melt(ad)


densityplot(~ as.numeric(value) | variable,
            data = ad,
            aspect = 1,
            col = "black",
            plot.points = FALSE,
            layout = c(4,3),
            scales=list(
              y=list(
                at=c(0.05,0.15, 0.25))),
            xlab = "Affect")




#######################
######################
#### Prop DK with legend
#######################
######################

pdk <- read.csv("meandk.csv")
pdk$person <- reorder(pdk$person, -pdk$est)

xyplot(person ~ est,
       groups = sig,
       data = pdk,
       aspect = 1.5,
       xlim = c(-.05, .85),
       pch = c(16,17), 
       xlab = "Proportion Don't Know\nParty Affiliation",
       ylab = "",
       key=list(space="right", cex=.75,
                title = "Influence on\nPublic Attitudes",
                points=list(pch=c(17,16)),
                text=list(c("Statistically\nSignificant","Not Statistically\nSignificant"))),
       panel = function (x, y,...) {
         panel.xyplot(x, y,col = "black",...)
         #panel.lines(x = 1.9112, y = -1:13, col = "grey", lty = 2)
         panel.lines(x = 0.3911111, y = -1:13, col = "grey", lty = 2)
         #panel.lines(x = 3.299, y = -1:13, col = "grey", lty = 3)
         panel.segments(pdk$lb, as.numeric(y),
                        pdk$ub, as.numeric(y), lty = 1, col = "black")} )

